<!DOCTYPE group PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<group>

<p><b>VLFeat</b> implements the fast image distance transform
  algorithm from <a href="#ref1">[1]</a>. The distance transform of an
  image <code>image</code> is defined as</p>
<pre>
  dt(u,v) = min  image(u',v') + alpha (u'-u-u0)^2 + beta (v'-v'-v0)^2
            u'v'
</pre>
<p>Usually, <code>image</code> is the response of a feature
detector. In this case, the distance transforms propagates the
response to nearby pixels, weighted by the distance. The distance
transform is used, for example, in the flexible parts model to quickly
determine the optimal configuration of a tree-like model by dynamic
programming. In this tutorial, the distance transform is used to
compute the distance of each pixel to the nearest edge.</p>

<p>As test data consider the edge map extracted by the MATLAB Canny edge
detector on one of VLFeat test images:</p>
<pre>
im = imread(fullfile(vl_root, 'data', 'a.jpg')) ;
im = im(1:100,1:100,:) ;
imSize = [size(im,1) size(im,2)] ;
edges = zeros(imSize) + inf;
edges(edge(rgb2gray(im), 'canny')) = 0 ;
</pre>

<div class="figure">
 <img src="%pathto:root;demo/imdisttf_src.jpg"/>
 <img src="%pathto:root;demo/imdisttf_edge.jpg"/>
 <div class="caption">
  <span class="content">
   Left: A source image. Right: Extracted Canny edges. Figure
   generated by <code>vl_demo_imdisttf</code>.
  </span>
 </div>
</div>

<p>The image has been cropped for ease of visualization; the image
distance transform itself is quite fast. Note that the edge map
has value zero where edges are present, and <code>inf</code> for
the other pixels. In this case, the image distance transform 
computes the closest edge point to each pixel, and its distance:</p>

<pre>
[distanceTransform, neighbors] = vl_imdisttf(single(edges)) ;
</pre>

<p><code>distanceTransform</code> contains the distance from each
pixel to the nearest edge element, and <code>neighbors</code> its
index. Note that, by default, <code>alpha=beta=1</code>
and <code>u0=v0=0</code>.</p>

<div class="figure">
 <img src="%pathto:root;demo/imdisttf_dist.jpg"/>
 <img src="%pathto:root;demo/imdisttf_neigh.jpg"/>
 <div class="caption">
  <span class="content">
   The distance to the closest edge element (left) and arrows
   connecting pixels to their closest edge element (right). Figure
   generated by <code>vl_demo_imdisttf</code>.
  </span>
 </div>
</div>


<h1>References</h1>
<ul>
  <li><p id="ref1">[1] P. F. Felzenszwalb and
      D. P. Huttenlocher. Distance transforms of sampled
      functions. Technical report, Cornell University, 2004.</p></li>
</ul>

</group>
